package org.footito.android.ui.views;

import org.footito.android.R;
import org.footito.android.connexion.FootitoConnexion;
import org.footito.android.data.Status;
import org.footito.android.tools.ConvertingTools;
import org.footito.android.tools.Tools;
import org.footito.android.ui.activity.TimelineFragment.OnStatusClickedListener;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

public class InformationView extends FrameLayout implements OnClickListener {

	private Status status;
	private ImageButton bSeeMore;
	private TextView tv_timestamp;
	private TextView tv_love; 
	private TextView tv_nolove;
	private ImageButton bShare;
	private ImageButton b_love;
	private ImageButton b_nolove;
	private OnStatusClickedListener mOnStatusClickedListatus;

	public InformationView(Context context, OnStatusClickedListener listener) {
		super(context);
		createView(context);
		mOnStatusClickedListatus = listener;
	}

	public InformationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		createView(context);
	}

	public InformationView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		createView(context);
	}

	private void createView(Context context) {
		LayoutInflater.from(context).inflate(R.layout.status_informations, this);
		bSeeMore = (ImageButton) findViewById(R.id.buttonMore);
		bSeeMore.setOnClickListener(this);
		bShare = (ImageButton) findViewById(R.id.buttonShare);
		bShare.setOnClickListener(this);
		tv_timestamp = (TextView) findViewById(R.id.TV_date);
		tv_love = (TextView) findViewById(R.id.TV_love);
		tv_nolove = (TextView) findViewById(R.id.TV_nolove);
		b_love = (ImageButton) findViewById(R.id.buttonLove);
		b_nolove = (ImageButton) findViewById(R.id.buttonNoLove);
	}

	public void setStatus(Status status) {
		this.status = status;
		tv_timestamp.setText(ConvertingTools.getFormattedDate(status.getTimestamp()));
		tv_love.setText(Long.toString(status.getPositiveLike()));
		tv_nolove.setText(Long.toString(status.getNegativeLike()));
		if (!FootitoConnexion.getInstance(getContext()).isConnected()) {
			b_love.setEnabled(false);
			b_nolove.setEnabled(false);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == bSeeMore) {
			if (mOnStatusClickedListatus != null)
				mOnStatusClickedListatus.onStatusClicked(status);
		} else if (v == bShare) {
			Tools.share(getContext(),
					status.getTeam().getName() + " \"" + status.getContent() + "\" " + status.getLink());
		}
	}

}
